class OrderItem < ActiveRecord::Base
  
  validates :order_id,
    :presence => true
  validates :ordered_count, 
    :numericality => {
      :greater_than_or_equal_to => 1
    }
  
  validates :has_been_sent_to_installer_count, 
    :installed_count,
    :after_serviced_count,
    :numericality => {
      :greater_than_or_equal_to => 0
    }
  
  belongs_to :furniture
  belongs_to :order
  belongs_to :feed
  has_many :feeds, :dependent => :destroy
  has_many :furniture_logistic_stat_order_items, :dependent => :destroy
  has_many :order_made_items

  def status 
    ActiveSupport::StringInquirer.new(feed.code)
  end

  # 重置状态feed
#  def reset_feed!
#    update_attribute :feed_id, Feed.where(:order_item_id => id).last.id
#  end
  
  def happened_on
    order.ordered_on
  end

  before_save do |order_item|
    if order_item.changes[:ordered_count].present?
      logger.debug "--------------------------------#{order_item.ordered_count}"
    end
  end
  
  def add_stat_order_item(stat_order_item)
    furniture_logistic_stat_order_items << stat_order_item
  end
  
  # 重置已预订数
  def reset_ordered_count!    
    self.ordered_count = FurnitureLogisticStatOrderItem.where(:order_item_id => id).sum(:ordered_count)
    save(:validate => false)
  end
  
  # 相关联的 stats  
  def furniture_logistic_stats
    furniture_logistic_stat_order_items.each_with_object([]) do |stat_order_item, stats|
      stats << stat_order_item.furniture_logistic_stat
    end
  end
  
  # 相关联的家具
  def furnitures
    furniture_logistic_stats.each_with_object([]) do |stat, furnitures|
      furnitures << stat.furniture if not furnitures.include?(stat.furniture)
    end
  end
  
  # 可被安装数
  def can_be_installed_count
    ordered_count - has_been_sent_to_installer_count - installed_count
  end
  
  # 重置 已派工数 = 派工状态的派工数累计数
  def reset_has_been_sent_to_installer_count!
    update_attribute :has_been_sent_to_installer_count, InstallItem.where("install_items.order_item_id = ?", id).has_been_sent_to_installer.sum('install_items.has_been_sent_to_installer_count')
  end
  
  # 重置 已安装数 = 完成或售后状态的已安装数
  def reset_installed_count!
#    install_items = InstallItem.where("install_items.order_item_id = ?", id).includes(:feed)
#    install_items.each do |item|
#      logger.debug "  item.feed.id=====#{item.feed.id}    item.feed.type======#{item.feed.type}   item.installed_count=======#{item.installed_count}   item.order_item.id=====#{item.order_item.id}"
#    end
#    Feed.all.each do |feed|
#      logger.debug "  feed.id=====#{feed.id}    feed.type======#{feed.type}"
#    end

    inst_count = InstallItem.where("install_items.order_item_id = ?", id).over.sum('install_items.installed_count')
    logger.debug "installed_count=======#{inst_count}"
    update_attribute :installed_count, inst_count
  end
  
  # 重置订做数
  def reset_order_made_count!
    update_attribute :order_made_count, OrderMadeItem.where('order_made_items.order_item_id = ?',id).sum('order_made_items.order_made_count')
  end
  
  def last_feed
    feeds.last
  end
  
end
